package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.common.constant.MessageConstant;
import com.atguigu.common.entity.PageResult;
import com.atguigu.common.entity.QueryPageBean;
import com.atguigu.common.entity.Result;
import com.atguigu.pojo.TravelItem;
import com.atguigu.service.TravelItemService;
import org.apache.xmlbeans.impl.jam.xml.TunnelledException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 自由行模块
 */
@RestController
@RequestMapping("/travelItem")
public class TravelItemController {

    @Reference
    TravelItemService travelItemService;

    //添加自由行
    @PostMapping("/add")
    public Result travelAdd(@RequestBody TravelItem travelItem){
        travelItemService.addTravelItem(travelItem);
        return new Result(true, MessageConstant.ADD_TRAVELITEM_SUCCESS);
    }

    //查询自由行列表
    @PostMapping("/list")
    public Result travelList(@RequestBody QueryPageBean queryPageBean){
        PageResult travelItems = travelItemService.selectTravelList(queryPageBean);
        return new Result(true,MessageConstant.QUERY_TRAVELITEM_SUCCESS,travelItems);
    }

    //查询自由行
    @GetMapping("/selectOne")
    public Result travelSelectOne(@RequestParam("id")Integer id){
        TravelItem travelItem = travelItemService.selectOne(id);
        return new Result(true,MessageConstant.QUERY_TRAVELITEM_SUCCESS,travelItem);
    }

    //删除自由行
    @DeleteMapping("/delete")
    public Result travelDelete(@RequestParam("id")Integer id){
        travelItemService.travelDelete(id);
        return new Result(true,MessageConstant.DELETE_TRAVELITEM_SUCCESS);
    }

    //修改自由行
    @PutMapping("/update")
    @PreAuthorize("hasAnyAuthority('TRAVELITEM_EDIT')")
    public Result travelEdit(@RequestBody TravelItem travelItem){
        travelItemService.travelEdit(travelItem);
        return new Result(true,MessageConstant.EDIT_TRAVELITEM_SUCCESS);
    }
    //查询所有自由行
    @GetMapping("/findAll")
    public Result findAll(){
        List<TravelItem> travelItems = travelItemService.findAll();
        return new Result(true,MessageConstant.QUERY_TRAVELITEM_SUCCESS,travelItems);
    }
}
